package co.za.itweb.itwebafrica;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;

@SuppressLint("SetJavaScriptEnabled")
public class Disquss extends Fragment{
	View myFragmentDisquss;
	
	WebView myWebView;
	 int width;
	 int height;
	 String articleID;
	 String articleTITLE;
	
	private String density2;

	private int bannerHolderWidth2;

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setRetainInstance(true);
	    DisplayMetrics metrics = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay()
				.getMetrics(metrics);
		
		 Bundle extras = getArguments();
//		 getActivity().getIntent().getExtras();
		 articleID = extras.getString("articleID");
		 articleTITLE=extras.getString("articleTITLE");
	
		articleTITLE= articleTITLE.replaceAll("\"","\\\"");
		articleTITLE= articleTITLE.replaceAll("\\?","~qm~");
		articleTITLE= articleTITLE.replaceAll("&","~amp~");
		
		
		
		 height = metrics.heightPixels;
		
		 width = metrics.widthPixels;
		
	}
	
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		
		 myFragmentDisquss= inflater.inflate(R.layout.fragmentdisquss,
				container, false);
		setRetainInstance(true);
		
		return myFragmentDisquss;
	}
	
	
	
//	public  int getDensitySize(float size) {
//	    float density = getResources().getDisplayMetrics().density;
//	    return (int)(density * size);
//	}
	
	@Override
	public void onStart(){
		super.onStart();
		RelativeLayout linLayout = (RelativeLayout) getView().findViewById(R.id.disquss);
		
		if(determineDensity()=="ldpi"){
			bannerHolderWidth2=(int) (int) (width+(width/3))-10;
		}else if(determineDensity()=="mdpi"){
			bannerHolderWidth2=(int) (width)-10;
		}else if(determineDensity()=="hdpi"){
			
			bannerHolderWidth2=(int)  (width-(width/3))-10;
			
		}else if(determineDensity()=="xdpi"){
			bannerHolderWidth2=(int)width+(width/2)-10;
		}
		RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(width,LayoutParams.WRAP_CONTENT);
		

		 myWebView = new WebView(getActivity()); 
		linLayout.addView(myWebView, layoutParams);
		
	 

		myWebView.getSettings().setJavaScriptEnabled(true);


		int currentapiVersion = android.os.Build.VERSION.SDK_INT;
		if (currentapiVersion <= android.os.Build.VERSION_CODES.GINGERBREAD){

		((WebView) myWebView).setWebViewClient(new WebViewClient() {

		}); 
		 ((WebView) myWebView).loadUrl("http://itweb.mobi/disqus-comments.php?id="+articleID+"&url=source=android&tag=article&id="+articleID+"&title="+articleTITLE);

		
		}else{
		
			((WebView) myWebView).setWebViewClient(new WebViewClient() {
			
				
			}); 
		
			  ((WebView) myWebView).loadData("<html><head><meta name=\"viewport\" content=\"minimum-scale=1.0; maximum-scale=1.0; user-scalable=false; initial-scale=1.0;\"/><style>html,body,iframe{margin:0;padding:0;}</style></head><body><iframe style=\"width:"+bannerHolderWidth2+"px;height:600px;border:0px;margin:0px;\" src=\"http://itweb.mobi/disqus-comments.php?id="+articleID+"&url=source=android&tag=article&id="+articleID+"&title="+articleTITLE+"\"></iframe></body></html>", "text/html", null);
			
		}
		
	
	}
	
	
	public String determineDensity(){
		switch (getResources().getDisplayMetrics().densityDpi) {
		case DisplayMetrics.DENSITY_LOW:
			density2="ldpi";
			
		    break;
		case DisplayMetrics.DENSITY_MEDIUM:
			density2= "mdpi";
			
			 break;
		case DisplayMetrics.DENSITY_HIGH:
			density2= "hdpi";
			
			 break;
		case DisplayMetrics.DENSITY_XHIGH:
			density2= "xdpi";
			 break;
		}
		return density2;
	}
	
	@SuppressWarnings("deprecation")
	public String getOrientation(Context context) {
		final int rotation = ((WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
				.getOrientation();
		switch (rotation) {
		case Surface.ROTATION_0:
			return "portrait";
		case Surface.ROTATION_90:
			return "landscape";
		case Surface.ROTATION_180:
			return "portrait";
		default:
			return "landscape";
		}

	}
	

	
		 
}
